<?php


namespace App\Jobs\Zhunya;

use App\Models\CacheModel\Orders as OrdersCacheModel;
use App\Models\CacheModel\OrdersLib;
use App\Models\CacheModel\privatearea\PrivateAreaOrders;
use Log;
use Json;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class CouponExpireCheckJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $jobData;

    /**
     * Create a new job instance.
     *
     * @param array $jobData
     */
    public function __construct(array $jobData)
    {
        $this->jobData = $jobData;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function task()
    {
      $jobData = $this->jobData;
        Log::write(":优惠券过期开始；ID【{$cid}】", 'log');

        $userCouponModel = new UserCoupon();
        try
        {
            $couponInfo = $userCouponModel->where('id', $cid)->field('status,end_time,uid,is_give')->find();
            $uid = (int)$couponInfo['uid'];
            $isGive = (int)$couponInfo['is_give'];
            $status = (int)$couponInfo['status'];
            Log::write("step1", 'log');
            $time = time();
            if ($couponInfo['end_time'] > $time)
            {   // 优惠券未过期，延迟执行
                $delayTime = $couponInfo['end_time'] - $time + 5;
                Log::write(":优惠券ID【{$cid}】还未过期；延期【{$delayTime}】秒重新执行过期。", 'log');
                $job->release($delayTime);
                return;
            }
            Log::write("step2", 'log');
            if ($status !== 0)
            {
                Log::write("setp3", 'log');
                $status = [
                    0 => '未使用',
                    1 => '已使用',
                    2 => '已赠与',
                    3 => '已过期失效',
                    4 => '已锁定',
                ];
                Log::write(":优惠券ID【{$cid}】{$status[$couponInfo['status']]}；队列关闭。", 'log');
                // 优惠券已消费 队列关闭
                $job->delete();
                return;
            }
            Log::write("step4", 'log');
            // 优惠券过期 并自动更新 优惠券详情缓存
            $userCouponModel->where('id', $cid)->update([
                'status' => 3,
                'update_time' => $time
            ]);
        }
        catch (Exception $e)
        {
            Log::write(":优惠券ID【{$cid}】过期失败；错误消息：{$e->getMessage()};队列延期30分钟执行", 'error');
            $job->release(1800);
            return;
        }
        Log::write("step5", 'log');

        //删除未使用内的缓存KEY
        $userCouponModel->listDataDel($uid, [$cid], 1);
        // 删除可赠与内的缓存KEY
        if ($isGive === 1)
        {
            $userCouponModel->listDataDel($uid, [$cid], 2);
        }
        // 新增已失效过期内的缓存
        $userCouponModel->listDataAdd($uid, [$cid => $time], 3);
        Log::write("step6", 'log');

        Log::write(":优惠券ID【{$cid}】过期成功；队列关闭", 'log');

        //$job->delete();
    }
}